package com.ai.nsa.listener;

import com.asiainfo.appserver.ContextListener;
import com.asiainfo.appserver.core.ServerConfiguration;
import com.asiainfo.its.ITSNode;
import com.asiainfo.its.ITSPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Feng Chen 2017/6/8.
 */
@Component
public class SNAListener implements ContextListener {

    @Autowired
    private List<ITSPool> itsPools;

    private static final Logger logger = LoggerFactory.getLogger(SNAListener.class);

    @Override
    public void initialize(ServerConfiguration configuration) {
        startupItsPool();
    }

    @Override
    public void destroy(ServerConfiguration configuration) {
        shutdownItsPool();
    }


    // SECTION: INNER HELPER

    private void startupItsPool() {
        logger.info("start its pool, node: {}", ITSNode.getId());

        if (itsPools == null || itsPools.size() <= 0) {
            return;
        }

        for (ITSPool itsPool : itsPools) {
            itsPool.startup();
        }
    }

    private void shutdownItsPool() {
        if (itsPools == null || itsPools.size() <= 0) {
            return;
        }

        for (ITSPool itsPool : itsPools) {
            itsPool.shutdown();
        }
    }

}
